package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author yangeit
 * @date 2023年11月23日 11:55
 * @Description
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;

    @Override
    @Transactional
    public void addCart(ShoppingCartDTO shoppingCartDTO) {

//        1.创建购物车
         ShoppingCart shoppingCart = new ShoppingCart();
//         赋值值
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
//        2. 设置用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());

//        3. 判断当前商品是否在购物车中(用户id，前端传过的条件：dishid setmealid
      List<ShoppingCart> shoppingCarts=shoppingCartMapper.list(shoppingCart);

//      4.判断是否存在
        if (shoppingCarts!=null && shoppingCarts.size()>0){
//            此商品存在，数量加1，执行update操作
             ShoppingCart shoppingCart1 = shoppingCarts.get(0);
             shoppingCart1.setNumber(shoppingCart1.getNumber()+1);
             shoppingCartMapper.updateNumberById(shoppingCart1);


        }else {
//            此商品不存在，插入购物车表中
             Long dishId = shoppingCartDTO.getDishId();
             if (dishId!=null){
//                 插入的是菜品，设置dish_id
                  Dish dish = dishMapper.getById(dishId);
                 shoppingCart.setName(dish.getName());
                 shoppingCart.setImage(dish.getImage());
                 shoppingCart.setAmount(dish.getPrice());

             }else {
//                 插入的是套餐，设置setmeal_id
                  Long setmealId = shoppingCart.getSetmealId();
                 final Setmeal setmeal = setmealMapper.getById(setmealId);
                 shoppingCart.setName(setmeal.getName());
                 shoppingCart.setImage(setmeal.getImage());
                 shoppingCart.setAmount(setmeal.getPrice());

             }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());


            shoppingCartMapper.insert(shoppingCart);


        }



    }

    @Override
    public List<ShoppingCart> list() {

         ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> shoppingCarts=shoppingCartMapper.list(shoppingCart);

        return shoppingCarts;
    }

    @Override
    public void deleteByUserId() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }


}
